package com.zhang.zhangapp.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhang.zhangapp.R;
import com.zhang.zhangapp.bean.user.BankCard;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

/**
 * Created by Administrator on 2018/4/25.
 */

public class BankAdapter extends BaseAdapter {

    private List<BankCard> bankCardList;
    private Context context;

    public BankAdapter(List<BankCard> bankCardList, Context context) {
        this.bankCardList = bankCardList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return bankCardList == null ? 0 : bankCardList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder vh = null;
        if (view == null) {
            vh = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.item_bank, null);
            x.view().inject(vh, view);
            view.setTag(vh);
        } else {
            vh = (ViewHolder) view.getTag();
        }
        BankCard card = bankCardList.get(i);

        vh.typeImageView.setImageResource(getImageSrcByCode(card.getCode()));
        vh.nameText.setText(getNameByCode(card.getCode()));
        vh.typeText.setText(card.getType());
        vh.cardNoText.setText(CAR_STR + getLast4Str(card.getBankNo()));
        vh.bgLayout.setBackgroundResource(getBackgroundByCode(card.getCode()));
        return view;
    }

    private final static String CAR_STR = "**** **** **** ";

    private int getBackgroundByCode(String code){
        if (code.equals("001"))
            return R.drawable.bank_red_bg;
        if (code.equals("004"))
            return R.drawable.bank_red_bg;
        if (code.equals("002"))
            return R.drawable.bank_blue_bg;
        if (code.equals("003"))
            return R.drawable.bank_green_bg;
        if (code.equals("010"))
            return R.drawable.bank_blue_bg;
        if (code.equals("005"))
            return R.drawable.bank_red_bg;
        if (code.equals("007"))
            return R.drawable.bank_blue_bg;
        if (code.equals("008"))
            return R.drawable.bank_green_bg;
        if (code.equals("009"))
            return R.drawable.bank_blue_bg;
        if (code.equals("006"))
            return R.drawable.bank_yellow_bg;
        if (code.equals("011"))
            return R.drawable.bank_green_bg;
        return R.drawable.bank_red_bg;
    }

    private String getNameByCode(String code) {
        if (code.equals("001"))
            return "中国银行";
        if (code.equals("004"))
            return"中国工商银行";
        if (code.equals("002"))
            return "中国建设银行";
        if (code.equals("003"))
            return "中国农业银行";
        if (code.equals("010"))
            return "交通银行";
        if (code.equals("005"))
            return "招商银行";
        if (code.equals("007"))
            return "兴业银行";
        if (code.equals("008"))
            return "民生银行";
        if (code.equals("009"))
            return "上海浦东发展银行";
        if (code.equals("006"))
            return "中国光大银行";
        if (code.equals("011"))
            return "中国邮政储蓄银行";
        return "其他";
    }

    private int getImageSrcByCode(String code) {
        if (code.equals("001"))
            return R.mipmap.china_bank;
        if (code.equals("004"))
            return R.mipmap.icbc_bank;
        if (code.equals("002"))
            return R.mipmap.construction_bank;
        if (code.equals("003"))
            return R.mipmap.agr_bank;
        if (code.equals("010"))
            return R.mipmap.communications_back;
        if (code.equals("005"))
            return R.mipmap.merchants_bank;
        if (code.equals("007"))
            return R.mipmap.industrial_bank;
        if (code.equals("008"))
            return R.mipmap.ltd_bank;
        if (code.equals("009"))
            return R.mipmap.pufa_bank;
        if (code.equals("006"))
            return R.mipmap.guangda_bank;
        if (code.equals("011"))
            return R.mipmap.youzheng_bank;
        return R.mipmap.no_icon_bank;
    }


    private String getLast4Str(String value) {
        if (value == null)
            return "";
        if (value.length() <= 4) {
            return value;
        }
        return value.substring(value.length() - 4, value.length() - 1);
    }

    class ViewHolder {
        @ViewInject(R.id.bank_card_image)
        ImageView typeImageView;
        @ViewInject(R.id.bank_name)
        TextView nameText;
        @ViewInject(R.id.bank_card_no)
        TextView cardNoText;
        @ViewInject(R.id.bank_type)
        TextView typeText;
        @ViewInject(R.id.bg_layout)
        LinearLayout bgLayout;
    }
}
